﻿using System;
using System.Xml;
using System.Data;
using System.Collections.ObjectModel;
using SharePointSearchBench.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Diagnostics;


namespace SharePointSearchBench.Tests
{
    [TestClass()]
    public class CamlConditionTest
    {
        [TestMethod()]
        public void TestTextEquality()
        {
            string field = "Title";
            string value = "blah";
            CamlCondition condition = CamlCondition.Equality(field, value);

            string expected = "<Eq><FieldRef Name=\"" + field + "\"/><Value Type=\"Text\">" + value + "</Value></Eq>";
            string actual = condition.ToString();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void TestTextEqualityWithAnd()
        {
            string field1 = "Title";
            string value1 = "blah";
            string field2 = "Author";
            string value2 = "halb";
            CamlCondition condition1 = CamlCondition.Equality(field1, value1);
            CamlCondition condition2 = CamlCondition.Equality(field2, value2);

            condition1.And(condition2);

            string expected = "<And><Eq><FieldRef Name=\"" + field1 + "\"/><Value Type=\"Text\">" + value1 + "</Value></Eq><Eq><FieldRef Name=\"" + field2 + "\"/><Value Type=\"Text\">" + value2 + "</Value></Eq></And>";
            string actual = condition1.ToString();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void TestTextEqualityWithOr()
        {
            string field1 = "Title";
            string value1 = "blah";
            string field2 = "Author";
            string value2 = "halb";
            CamlCondition condition1 = CamlCondition.Equality(field1, value1);
            CamlCondition condition2 = CamlCondition.Equality(field2, value2);

            condition1.Or(condition2);

            string expected = "<Or><Eq><FieldRef Name=\"" + field1 + "\"/><Value Type=\"Text\">" + value1 + "</Value></Eq><Eq><FieldRef Name=\"" + field2 + "\"/><Value Type=\"Text\">" + value2 + "</Value></Eq></Or>";
            string actual = condition1.ToString();
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void TestTextEqualityWithTwoAnds()
        {
            string field1 = "Title";
            string value1 = "blah";
            string field2 = "Author";
            string value2 = "halb";
            string field3 = "Chicken";
            string value3 = "Kitty";
            CamlCondition condition1 = CamlCondition.Equality(field1, value1);
            CamlCondition condition2 = CamlCondition.Equality(field2, value2);
            CamlCondition condition3 = CamlCondition.Equality(field3, value3);

            condition2.And(condition3);
            condition1.And(condition2);

            string expected = "<And><Eq><FieldRef Name=\"" + field1 + "\"/><Value Type=\"Text\">" + value1 + "</Value></Eq><And><Eq><FieldRef Name=\"" + field2 + "\"/><Value Type=\"Text\">" + value2 + "</Value></Eq><Eq><FieldRef Name=\"" + field3 + "\"/><Value Type=\"Text\">" + value3 + "</Value></Eq></And></And>";
            string actual = condition1.ToString();
            Assert.AreEqual(expected, actual);
        }

    }
}
